package com.yidu.qianfeng.delivery;

import com.yidu.qianfeng.common.bean.company.Subst;
import com.yidu.qianfeng.common.bean.waybill.Waybill;
import com.yidu.qianfeng.controller.UserController;
import com.yidu.qianfeng.service.ShuntServiceInter;
import com.yidu.qianfeng.service.TrackWaybillService;
import com.yidu.qianfeng.service.impl.ShuntServiceImpl;
import com.yidu.qianfeng.service.impl.TrackWaybillServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 陈峰
 * @Description:
 * @Date Created in 9:21 2021/4/21
 * @since 1.0
 */
public class DeliveryController {

    @Resource
    private ShuntServiceInter shuntServiceInter;
    private ShuntServiceImpl shuntServiceImpl;

    @Resource
    private TrackWaybillService trackWaybillService;
    @Resource
    private TrackWaybillServiceImpl trackWaybillServiceImpl;

    @RequestMapping("/queryDelivery_c")
    @ResponseBody
    public Map<String, Object> loadDelivery(String number){

//        System.out.println(number);
        List<Waybill> list=new ArrayList<>();
        Map<String, Object> map = new HashMap<String, Object>();

        if (number!=null) {
            String[] split = number.split(",");
            for (int i = 0; i < split.length; i++) {
                Waybill waybill = shuntServiceInter.queryDelivery(split[i]);
                System.out.println("www"+waybill.getWaybillCargoName());
                if(waybill!=null){
                    list.add(waybill);
                }
            }
        }
//        System.out.println("delivery list.size()+++"+list.size());
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("/deliveryUpType")
    @ResponseBody
    public  Map<String, Object> deliveryUpdateType(String deliveryId,String number,String carNumber,double money, HttpServletRequest request){
//        System.out.println("delivery Update Type :"+number);

        Map<String, Object> map = new HashMap<String, Object>();
        HashMap<String, Object> addMap = new HashMap<String, Object>();
        addMap.put("deliveryId",deliveryId);
        addMap.put("carNumber",carNumber);
        addMap.put("money",money);
        addMap.put("number" ,number);
        //核销人
        if (number!=null){
            String[] split = number.split(",");
            for (int i = 0; i < split.length; i++) {
                Waybill bill =new Waybill();
                bill.setWaybillConsignNum(split[i]);
                Subst subst = shuntServiceInter.selectSubst(UserController.getLoginEmpInfo(request).getEmpSubstId());
//                System.out.println("bill.setWaybillConsignNum(split[i]);----"+bill.getWaybillConsignNum());
                //新增跟踪
                trackWaybillServiceImpl.generateTrackDelivery(bill,null,UserController.getLoginEmpInfo(request));
                bill.setWaybillConsignNum("");
            }
            int addDelivery = shuntServiceInter.addDelivery(addMap);
//            System.out.println("boolean addDelivery : "+addDelivery);
        }
        return map;
    }

}
